package leetcode.editor.cn.common;

/**
 * @Author: Dempsey
 * @Date: 2021/1/5 14:04
 * @Description:
 */
public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        ListNode that = this;
        while (that.next != null) {
            sb.append(that.val + ",");
            that = that.next;
        }
        sb.append(that.val + ",");
        return sb.toString();
    }

    public static ListNode getListNodeByArray(int[] array){
        ListNode ans = new ListNode(array[0]);
        ListNode tail = ans;
        for (int i = 1; i < array.length; i++) {
            tail.next = new ListNode(array[i]);
            tail = tail.next;
        }
        return ans;
    }
}
